package com.capgemini.simpleappblanche.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import com.capgemini.simpleappblanche.domain.customer.Customer;

@Getter
@Setter
@ToString(of={"id"})
@EqualsAndHashCode(of={"id"})
@Entity
@Table(name = "AGENCY")
@NamedQueries({
    @NamedQuery(name = "agency.allActive", query = "from Agency a where a.active = true order by a.name"),
    @NamedQuery(name = "agency.activeByIds", query = "from Agency a where a.active = true and a.id in :agencyIds order by a.name")
})
@SequenceGenerator(name = "agency_id_generator", sequenceName = "SEQUENCE_AGENCY", allocationSize = 1)
public class Agency {
	
	// Technical id
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "agency_id_generator")
	@Column(name = "AGENCY_ID")
	private Long id;
	
	@Column( name = "NAME" ,length = 50, nullable = false)
	private String name;

	@Column( name = "ACTIVE" , nullable = false)
    private Boolean active;
	
//	@OneToMany(mappedBy="agency")
//	public List<Profession> professions;
    
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(
            name = "AGENCY_CUSTOMER", 
            joinColumns = { @JoinColumn(name = "AGENCY_ID", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "CUSTOMER_ID", nullable = false, updatable = false) })
    private List<Customer> customers;
}
